package com.yingqi.controller;

import com.yingqi.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentCtrl {
    /*只能处理post请求
   等同@RequestMapping(value = "/save",method = RequestMethod.POST)*/
   @PostMapping("/save")
    /*只能处理get请求
    等同@RequestMapping(value = "/save",method = RequestMethod.GET)*/
    //@GetMapping("/save")
    public String save(@RequestParam(value = "name",required = false) String name){
       System.out.printf("name:"+name);
        return "success";//保存成功
    }
    @RequestMapping("/deleteById")
    public String deleteById(String id){
        System.out.printf("id:"+id);
        return "del_success";//删除成功
    }
    @RequestMapping("/deleteByIds")
    public String deleteByIds(String[] ids){
        for (String id:ids){
            System.out.printf("id:"+id);
        }
        return "student/del_success";//删除成功
    }
    @RequestMapping("/deleteByIds2")
    public String deleteByIds2(@RequestParam("ids") List<String> ids){
        for (String id:ids){
            System.out.printf("id:"+id);
        }
        return "student/del_success";//删除成功
    }
    public void testApi(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
        String id=request.getParameter("id");
        request.getRequestDispatcher("/success.jsp").forward(request,response);
    }
    @PostMapping("/save2")
    public String save2(Student student){
        System.out.println(student);
        return "success";//保存成功
    }
    @RequestMapping("/showById")
    public String showById(String id, Model model){
        Student std=new Student();
        std.setName("李雷");
        std.setAddress("北京");
        model.addAttribute("student",std);
        return "student/detail";
    }
    @RequestMapping("/showById2")
    public ModelAndView showById2(String id){
        Student std=new Student();
        std.setName("李雷");
        std.setAddress("北京");
        ModelAndView mv=new ModelAndView();
        mv.setViewName("student/detail");
        mv.addObject("student",std);
        return mv;
    }
    public String updateById(){
        return "upd_success";//更新成功
    }
    public String query(){
        return "List";
    }
}
